﻿namespace FIMTestCasesDriver
{
    using System.Management;

    public static class RunProfileInvoker
    {
        private const string FimSyncServiceWMINameSpace = "root\\MicrosoftIdentityIntegrationServer";
        private const string FimSyncServiceMAObjectSpace = "MIIS_ManagementAgent.Name";
        private const string FullImportAndFullSyncProfileName = "full import and full sync";
        private const string DeltaImportAndDeltaSyncProfileName = "delta import and delta sync";
        private const string ExportAndDeltaImportStageOnlyProfileName = "export and delta import stage only";
        private const string ActiveDirectoryMAName = "ARS";
        private const string ADLDSMAName = "ADLDS";
        private const string HRMAName = "HR";
        private const string RegistrationMAName = "Registration";

        public static void RunFullCycle()
        {
            RunProfile(
                ActiveDirectoryMAName,
                FullImportAndFullSyncProfileName);

            RunProfile(
                RegistrationMAName,
                FullImportAndFullSyncProfileName);

            RunProfile(
                HRMAName,
                FullImportAndFullSyncProfileName);

            RunProfile(
                ActiveDirectoryMAName,
                ExportAndDeltaImportStageOnlyProfileName);

            RunProfile(
                ADLDSMAName,
                ExportAndDeltaImportStageOnlyProfileName);
        }

        private static void RunProfile(
            string maName,
            string runProfileName)
        {
            var managementObject = new ManagementObject(
                FimSyncServiceWMINameSpace,
                string.Format("{0}=" + "'{1}'", FimSyncServiceMAObjectSpace, maName),
                null);
            var inParameters = managementObject.GetMethodParameters("Execute");
            inParameters["RunProfileName"] = runProfileName;
            managementObject.InvokeMethod("Execute", inParameters, null);
        }
    }
}
